/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.flexengine.server.database;

import com.flexengine.logger.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 * @author Luann R. Athayde
 * @version 1.0
 * @since 1.0
 */
public class FlexEngineEntityManagerFactory {
    
    private static EntityManagerFactory emf;
    private static EntityManager em = null;
    
    private static String        persistenceUnit;

    public static void init() {
        try {
            emf = Persistence.createEntityManagerFactory(getPersistenceUnit());
            em = emf.createEntityManager();
        } catch (Exception e) {
            emf = null;
            em = null;
            Logger.logError("FlexEngineEntityManagerFactory::init(): "+e);
        }
    }

    public static String getPersistenceUnit() {
        return persistenceUnit;
    }

    public static void setPersistenceUnit(String persistenceUnit) {
        FlexEngineEntityManagerFactory.persistenceUnit = persistenceUnit;
    }
    
    public static EntityManager getDefaultEntityManager() {
        return em;
    }
    
    public static EntityManager getEntityManager() {
        if( emf!=null ) {
            return emf.createEntityManager();
        }
        return null;
    }
    
}
